package com.woniu.his.dms.app.apiImpl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniu.his.dms.client.DiseCataLogClient;
import com.woniu.his.dms.client.param.CreateCatLogParam;
import com.woniu.his.dms.client.result.DmsDiseCatalogResult;
import com.woniu.his.dms.infra.model.DiseCatalog;
import com.woniu.his.dms.service.DiseCatalogService;
import com.woniu.his.result.CommonResultCode;
import com.woniu.his.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

/**
 * @version: java version 1.8
 * @Author: Mr tao
 * @description:
 * @date: 2022-11-27 22:33
 */
@Slf4j
@RequestMapping("/dms")
@RestController
public class DiseCataLogApiImpl implements DiseCataLogClient {

    @Resource
    private DiseCatalogService DiseCatalogService;


    @Override
    public Result createCataLog(CreateCatLogParam param) {
       log.info("新增一个诊断目录:{}",param);
        if(param.getId()==null){
            DiseCatalog diseCatalog = BeanUtil.copyProperties(param, DiseCatalog.class);
            diseCatalog.setStatusDiseCatalog("ON");
            DiseCatalogService.save(diseCatalog);
            return Result.getInstance();
        }
        return Result.getInstance().setCode(CommonResultCode.GENERAL_FAIL.getCode()).setMsg("新增失败");
    }


    @Override
    public Result deleteCataLog(List<Long> ids) {
        log.info("删除目录:{}",ids);
        Collection<DiseCatalog> diseCatalogs = DiseCatalogService.listByIds(ids);
        if(diseCatalogs.size()<=0) return  Result.getInstance(CommonResultCode.GENERAL_FAIL).setMsg("目录不存在");
        for (DiseCatalog diseCatalog : diseCatalogs) {
            diseCatalog.setStatusDiseCatalog("OFF");
        }
        DiseCatalogService.updateBatchById(diseCatalogs);
        return Result.getInstance();
    }


    /**
     * 修改目录
     * @param param
     * @return
     */
    @Override
    public Result updateCataLog(CreateCatLogParam param) {
        log.info("修改目录:{}",param);
        DiseCatalog diseCatalog = BeanUtil.copyProperties(param, DiseCatalog.class);
        boolean b = DiseCatalogService.updateById(diseCatalog);
        return b ? Result.getInstance() : Result.getInstance(CommonResultCode.GENERAL_FAIL).setMsg("修改失败");
    }


    @Override
    public Result<List<DmsDiseCatalogResult>> listAllCatalog() {
        log.info("查询全部目录:{}");
        List<DiseCatalog> list = DiseCatalogService.list(new QueryWrapper<DiseCatalog>().eq("status_dise_catalog", "ON"));
       if(list.size()<=0) return null;
        return Result.getInstanceList(DmsDiseCatalogResult.class).setData(BeanUtil.copyToList(list,DmsDiseCatalogResult.class));
    }
}
